<?php

/**
 * 流程
 */

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');
require(ROOT_PATH . 'includes/lib_order.php');
require(ROOT_PATH . 'includes/cls_image.php');

/* 载入语言文件 */
require_once(ROOT_PATH . 'languages/' .$_CFG['lang']. '/user.php');
require_once(ROOT_PATH . 'languages/' .$_CFG['lang']. '/shopping_flow.php');
/*------------------------------------------------------ */
//-- INPUT
/*------------------------------------------------------ */

if (!isset($_REQUEST['step']))
{
    $_REQUEST['step'] = "checkout";
}

/*------------------------------------------------------ */
//-- PROCESSOR
/*------------------------------------------------------ */

assign_template();
assign_dynamic('flow');
$position = assign_ur_here(0, $_LANG['shopping_flow']);
$smarty->assign('page_title',       $position['title']);    // 页面标题
$smarty->assign('ur_here',          $position['ur_here']);  // 当前位置

$smarty->assign('categories',       get_categories_tree()); // 分类树
$smarty->assign('helps',            get_shop_help());       // 网店帮助
$smarty->assign('lang',             $_LANG);
$smarty->assign('show_marketprice', $_CFG['show_marketprice']);
$smarty->assign('data_dir',    DATA_DIR);       // 数据目录

/*------------------------------------------------------ */
//-- 添加到购物车
/*------------------------------------------------------ */
if ($_REQUEST['step'] == 'essaytocart')
{
	$title    = isset($_POST["title"]) ? htmlspecialchars(trim($_POST["title"])) : "";
	$content  = isset($_POST["content"]) ? htmlspecialchars(trim($_POST["content"])) : "";
	$wordnumber = isset($_POST["wordnumber"]) ? intval($_POST["wordnumber"]) : '';
	$retime = isset($_POST['retime']) ? intval($_POST["retime"]) : 1;
	$essayfile = $_FILES["essayfile"];
	$resume    = $_FILES["resume"];
	$source = isset($_POST["source"]) ? trim($_POST["source"]) : "mba";
	$id = isset($_POST['id']) ? intval($_POST['id']) : 0;
	$act = isset($_POST['act']) ? trim($_POST['act']) : '';
	$essay_file = '';
	$resume_file = '';
	if(empty($title))
	{
		show_message("标题不能为空");
	}
	if(empty($content))
	{
		show_message("内容不能为空");
	}
	
	$info = final_price($wordnumber, "essay");
    $data = array(
    	"type"         => "essay",
    	'session_id'   => SESS_ID,
    	"title"        => $title,
    	'content'      => $content,
    	'wordnumber'   => $wordnumber,
    	'retime'       => $retime,
    	'user_id'      => $_SESSION["user_id"],
    	'source'       => $source,
    	'price'        => $info["base_price"],
    	'reprice'      => $retime == 2 ? $info["reprice"] : 0,
    	'service_name' => "Essay 内容深度修改",
    );
         
    	$file = get_temp_file("essay");
    	if($file["essayfile"])
    	{
    		modify_cart($id, "essay_file");
            $data["essay_file"] = $res;
    	}
		if($file["resume_file"])
		{
			modify_cart($id, "resume_file");
            $data["resume_file"] = $res;
		}
		
    $where = "";
    if($act == "update")
    {
    	$action = "UPDATE";
    	$where = "rec_id = '{$id}' AND session_id = '".SESS_ID."'";
    }
    else
    {
    	$action = "INSERT";
    }
    $db->autoExecute($ecs->table("cart"), $data, $action, $where);
    clear_temp_file("essay");
    ecs_header("Location:flow.php?step=checkout");
}

/*------------------------------------------------------ */
//-- 添加到购物车
/*------------------------------------------------------ */
if ($_REQUEST['step'] == 'chenshutocart')
{
	$title    = isset($_POST["title"]) ? htmlspecialchars(trim($_POST["title"])) : "";
	$content  = isset($_POST["content"]) ? htmlspecialchars(trim($_POST["content"])) : "";
	$wordnumber = isset($_POST["wordnumber"]) ? intval($_POST["wordnumber"]) : '';
	$retime = isset($_POST['retime']) ? intval($_POST["retime"]) : 1;
	$essayfile = $_FILES["essayfile"];
	$resume    = $_FILES["resume"];
	$source = isset($_POST["source"]) ? trim($_POST["source"]) : "mba";
	$essay_file = '';
	$resume_file = '';
	if(empty($title))
	{
		show_message("标题不能为空");
	}
	if(empty($content))
	{
		show_message("内容不能为空");
	}
	$sql = "SELECT maxsize FROM " . $ecs->table("service") . " WHERE type = 'chenshu'";
	$maxsize = $db->getOne($sql);
	$maxsize = $maxsize * 1024 * 1024;
	$info = final_price($wordnumber, "chenshu");
    $data = array(
    	"type"         => "chenshu",
    	'session_id'   => SESS_ID,
    	"title"        => $title,
    	'content'      => $content,
    	'wordnumber'   => $wordnumber,
    	'retime'       => $retime,
    	'user_id'      => $_SESSION["user_id"],
    	'source'       => $source,
    	'price'        => $info["base_price"],
    	'reprice'      => $retime == 2 ? $info["reprice"] : 0,
    	'service_name' => "个人陈述内容深度修改",
    );
    
        $file = get_temp_file("chenshu");
    	if($file["essayfile"])
    	{
    		modify_cart($id, "essay_file");
            $data["essay_file"] = $res;
    	}
		if($file["resume_file"])
		{
			modify_cart($id, "resume_file");
            $data["resume_file"] = $res;
		}
    
    $db->autoExecute($ecs->table("cart"), $data, "INSERT");
    clear_temp_file("chenshu");
    ecs_header("Location:flow.php?step=checkout");
}
if ($_REQUEST['step'] == 'resumetocart')
{
	//$title    = isset($_POST["title"]) ? htmlspecialchars(trim($_POST["title"])) : "";
	$content  = isset($_POST["content"]) ? htmlspecialchars(trim($_POST["content"])) : "";
	//$wordnumber = isset($_POST["wordnumber"]) ? intval($_POST["wordnumber"]) : '';
	$retime = isset($_POST['retime']) ? intval($_POST["retime"]) : 1;
	//$essayfile = $_FILES["essayfile"];
	$resumefile    = $_FILES["resumefile"];
	$source = isset($_POST["source"]) ? trim($_POST["source"]) : "mba";
	//$essay_file = '';
	//$resume_file = '';
//	if(empty($title))
//	{
//		show_message("标题不能为空");
//	}
//	if(empty($content))
//	{
//		show_message("内容不能为空");
//	}
    $sql = "SELECT maxsize FROM " . $ecs->table("service") . " WHERE type = 'resume'";
	$maxsize = $db->getOne($sql);
	$maxsize = $maxsize * 1024 * 1024;
	$info = final_price($wordnumber, "resume");
    $data = array(
    	"type"         => "resume",
    	'session_id'   => SESS_ID,
    	//"title"        => $title,
    	'content'      => $content,
    	//'wordnumber'   => $wordnumber,
    	'retime'       => $retime,
    	'user_id'      => $_SESSION["user_id"],
    	'source'       => $source,
    	'price'        => $info["base_price"],
    	'reprice'      => $retime == 2 ? $info["reprice"] : 0,
    	'service_name' => "MBA/Master 简历推荐信润色",
    );
    
    	$file = get_temp_file("resume");
    	if($file["resumefile"])
    	{
    		modify_cart($id, "resume_file");
            $data["resume_file"] = $res;
    	}
    
    $db->autoExecute($ecs->table("cart"), $data, "INSERT");
    clear_temp_file("resume");
    ecs_header("Location:flow.php?step=checkout");
}

if ($_REQUEST['step'] == 'alltocart')
{
	//$title    = isset($_POST["title"]) ? htmlspecialchars(trim($_POST["title"])) : "";
	$content  = isset($_POST["content"]) ? htmlspecialchars(trim($_POST["content"])) : "";
	$remark  = isset($_POST["remark"]) ? htmlspecialchars(trim($_POST["remark"])) : "";
	//$wordnumber = isset($_POST["wordnumber"]) ? intval($_POST["wordnumber"]) : '';
	$mobile = isset($_POST['mobile']) ? trim($_POST["mobile"]) : "";
	$email = isset($_POST['email']) ? trim($_POST["email"]) : "";
	//$essayfile = $_FILES["essayfile"];
	$resumefile    = $_FILES["resumefile"];
	$source = isset($_POST["source"]) ? trim($_POST["source"]) : "mba";
	//$essay_file = '';
	//$resume_file = '';
//	if(empty($title))
//	{
//		show_message("标题不能为空");
//	}
//	if(empty($content))
//	{
//		show_message("内容不能为空");
//	}
    $sql = "SELECT maxsize FROM " . $ecs->table("service") . " WHERE type = 'all'";
	$maxsize = $db->getOne($sql);
	$maxsize = $maxsize * 1024 * 1024;
	$info = final_price($wordnumber, "all");
    $data = array(
    	"type"         => "all",
    	'session_id'   => SESS_ID,
    	//"title"        => $title,
    	'content'      => $content,
    	//'wordnumber'   => $wordnumber,
    	//'retime'       => $retime,
    	'user_id'      => $_SESSION["user_id"],
    	'source'       => $source,
    	'price'        => $info["base_price"],
    	'reprice'      => $retime == 2 ? $info["reprice"] : 0,
    	'service_name' => "全套服务申请",
    	'remark'       => $remark,
    	'mobile'       => $mobile,
    	'email'       => $email,
    );
    
    	$file = get_temp_file("all");
    	if($file["resumefile"])
    	{
    		modify_cart($id, "resume_file");
            $data["resume_file"] = $res;
    	}
    
    $db->autoExecute($ecs->table("cart"), $data, "INSERT");
    clear_temp_file("all");
    ecs_header("Location:flow.php?step=checkout");
}
if ($_REQUEST['step'] == 'multipletocart')
{
	//$title    = isset($_POST["title"]) ? htmlspecialchars(trim($_POST["title"])) : "";
	$content  = isset($_POST["content"]) ? htmlspecialchars(trim($_POST["content"])) : "";
	$contact_type  = isset($_POST["contact_type"]) ? htmlspecialchars(trim($_POST["contact_type"])) : "";
	$contact_time  = isset($_POST["contact_time"]) ? htmlspecialchars(trim($_POST["contact_time"])) : "";
	$contact  = isset($_POST["contact"]) ? htmlspecialchars(trim($_POST["contact"])) : "";
	//$wordnumber = isset($_POST["wordnumber"]) ? intval($_POST["wordnumber"]) : '';
//	$mobile = isset($_POST['mobile']) ? trim($_POST["mobile"]) : "";
//	$email = isset($_POST['email']) ? trim($_POST["email"]) : "";
	//$essayfile = $_FILES["essayfile"];
	$resumefile    = $_FILES["resumefile"];
	$source = isset($_POST["source"]) ? trim($_POST["source"]) : "mba";
	//$essay_file = '';
	//$resume_file = '';
//	if(empty($title))
//	{
//		show_message("标题不能为空");
//	}
//	if(empty($content))
//	{
//		show_message("内容不能为空");
//	}
    $sql = "SELECT maxsize FROM " . $ecs->table("service") . " WHERE type = 'multiple'";
	$maxsize = $db->getOne($sql);
	$maxsize = $maxsize * 1024 * 1024;
	$info = final_price($wordnumber, "multiple");
    $data = array(
    	"type"         => "multiple",
    	'session_id'   => SESS_ID,
    	//"title"        => $title,
    	'content'      => $content,
    	//'wordnumber'   => $wordnumber,
    	//'retime'       => $retime,
    	'user_id'      => $_SESSION["user_id"],
    	'source'       => $source,
    	'price'        => $info["base_price"],
    	//'reprice'      => $retime == 2 ? $info["reprice"] : 0,
    	'service_name' => "综合咨询服务申请",
    	'contact'       => $contact,
    	'contact_time'       => $contact_time,
    	'contact_type'       => $contact_type,
    );
    
    	$file = get_temp_file("multiple");
    	if($file["resumefile"])
    	{
    		modify_cart($id, "resume_file");
            $data["resume_file"] = $res;
    	}
    
    $db->autoExecute($ecs->table("cart"), $data, "INSERT");
    clear_temp_file("multiple");
    ecs_header("Location:flow.php?step=checkout");
}

if ($_REQUEST['step'] == 'quiztocart')
{
	//$title    = isset($_POST["title"]) ? htmlspecialchars(trim($_POST["title"])) : "";
	$content  = isset($_POST["content"]) ? htmlspecialchars(trim($_POST["content"])) : "";
	$contact_type  = isset($_POST["contact_type"]) ? htmlspecialchars(trim($_POST["contact_type"])) : "";
	$contact_time  = isset($_POST["contact_time"]) ? htmlspecialchars(trim($_POST["contact_time"])) : "";
	$contact  = isset($_POST["contact"]) ? htmlspecialchars(trim($_POST["contact"])) : "";
	$school  = isset($_POST["school"]) ? htmlspecialchars(trim($_POST["school"])) : "";
	$remark  = isset($_POST["remark"]) ? htmlspecialchars(trim($_POST["remark"])) : "";
	$major  = isset($_POST["major"]) ? htmlspecialchars(trim($_POST["major"])) : "";
	//$wordnumber = isset($_POST["wordnumber"]) ? intval($_POST["wordnumber"]) : '';
//	$mobile = isset($_POST['mobile']) ? trim($_POST["mobile"]) : "";
//	$email = isset($_POST['email']) ? trim($_POST["email"]) : "";
	//$essayfile = $_FILES["essayfile"];
	$resumefile    = $_FILES["resumefile"];
	$source = isset($_POST["source"]) ? trim($_POST["source"]) : "mba";
	//$essay_file = '';
	//$resume_file = '';
//	if(empty($title))
//	{
//		show_message("标题不能为空");
//	}
//	if(empty($content))
//	{
//		show_message("内容不能为空");
//	}
    $sql = "SELECT maxsize FROM " . $ecs->table("service") . " WHERE type = 'quiz'";
	$maxsize = $db->getOne($sql);
	$maxsize = $maxsize * 1024 * 1024;
	$info = final_price($wordnumber, "quiz");
    $data = array(
    	"type"         => "quiz",
    	'session_id'   => SESS_ID,
    	//"title"        => $title,
    	'content'      => $content,
    	//'wordnumber'   => $wordnumber,
    	//'retime'       => $retime,
    	'user_id'      => $_SESSION["user_id"],
    	'source'       => $source,
    	'price'        => $info["base_price"],
    	//'reprice'      => $retime == 2 ? $info["reprice"] : 0,
    	'service_name' => "模拟面试服务",
    	'contact'       => $contact,
    	'contact_time'       => $contact_time,
    	'contact_type'       => $contact_type,
    	'remark'             => $remark,
    	'major'              => $major, 
    	'school'             => $school,
    ); 
    
    	$file = get_temp_file("quiz");
    	if($file["resumefile"])
    	{
    		modify_cart($id, "resume_file");
            $data["resume_file"] = $res;
    	}
    
    $db->autoExecute($ecs->table("cart"), $data, "INSERT");
    clear_temp_file("quiz");
    ecs_header("Location:flow.php?step=checkout");
}
elseif ($_REQUEST['step'] == 'checkout')
{
    /*------------------------------------------------------ */
    //-- 订单确认
    /*------------------------------------------------------ */

    /* 检查购物车中是否有商品 */
    $sql = "SELECT COUNT(*) FROM " . $ecs->table('cart') .
        " WHERE session_id = '" . SESS_ID . "'";

    if ($db->getOne($sql) == 0)
    {
        show_message("对不起没有选择任何服务", '', 'index.php', 'error');
    }

    /*
     * 检查用户是否已经登录
     * 如果用户已经登录了则检查是否有默认的收货地址
     * 如果没有登录则跳转到登录和注册页面
     */
    if ($_SESSION['user_id'] == 0)
    {
        /* 用户没有登录且没有选定匿名购物，转向到登录页面 */
        ecs_header("Location: user.php?act=login&back_act=flow.php\n");
        exit;
    }
    
    /* 对商品信息赋值 */
    $service_list = cart_service(); // 取得商品列表，计算合计
    //print_r($service_list);
    $smarty->assign('service_list', $service_list);

    /*
     * 计算订单的费用
     */
    $order = flow_order_info();
    $total = order_fee($service_list);

    //print_r($total);
    $smarty->assign('total', $total);

    $payment_list = available_payment_list(1, $cod_fee);
    if(isset($payment_list))
    {
        foreach ($payment_list as $key => $payment)
        {
            if ($payment['is_cod'] == '1')
            {
                $payment_list[$key]['format_pay_fee'] = '<span id="ECS_CODFEE">' . $payment['format_pay_fee'] . '</span>';
            }
            /* 如果有易宝神州行支付 如果订单金额大于300 则不显示 */
            if ($payment['pay_code'] == 'yeepayszx' && $total['amount'] > 300)
            {
                unset($payment_list[$key]);
            }
            /* 如果有余额支付 */
            if ($payment['pay_code'] == 'balance')
            {
                /* 如果未登录，不显示 */
                if ($_SESSION['user_id'] == 0)
                {
                    unset($payment_list[$key]);
                }
                else
                {
                    if ($_SESSION['flow_order']['pay_id'] == $payment['pay_id'])
                    {
                        $smarty->assign('disable_surplus', 1);
                    }
                }
            }
        }
    }
    $smarty->assign('payment_list', $payment_list);

    $user_info = user_info($_SESSION['user_id']);
	
    /* 保存 session */
    $_SESSION['flow_order'] = $order;
}
elseif ($_REQUEST['step'] == 'select_payment')
{
    /*------------------------------------------------------ */
    //-- 改变支付方式
    /*------------------------------------------------------ */
    include_once('includes/cls_json.php');
    $json = new JSON;
    $result = array('error' => '', 'content' => '', 'need_insure' => 0, 'payment' => 1);

    /* 对商品信息赋值 */
    $cart_goods = cart_service(); // 取得商品列表，计算合计

    if (empty($cart_goods))
    {
        $result['error'] = "没有任何服务";
    }
    else
    {
        /* 取得购物流程设置 */
        $smarty->assign('config', $_CFG);

        /* 取得订单信息 */
        $order = flow_order_info();
        $order['pay_id'] = intval($_REQUEST['payment']);
        $payment_info = payment_info($order['pay_id']);
        $result['pay_code'] = $payment_info['pay_code'];

        /* 保存 session */
        $_SESSION['flow_order'] = $order;

        /* 计算订单的费用 */
        $total = order_fee($cart_goods);
        $smarty->assign('total', $total);

        /* 取得可以得到的积分和红包 */
       // $smarty->assign('total_integral', cart_amount(false, $flow_type) - $total['bonus'] - $total['integral_money']);
       // $smarty->assign('total_bonus',    price_format(get_total_bonus(), false));

        /* 团购标志 */
        if ($flow_type == CART_GROUP_BUY_GOODS)
        {
            $smarty->assign('is_group_buy', 1);
        }

        $result['content'] = $smarty->fetch('library/order_total.lbi');
    }

    echo $json->encode($result);
    exit;
}
/*------------------------------------------------------ */
//-- 完成所有订单操作，提交到数据库
/*------------------------------------------------------ */
elseif ($_REQUEST['step'] == 'done')
{
    include_once('includes/lib_clips.php');
    include_once('includes/lib_payment.php');

    /* 检查购物车中是否有商品 */
    $sql = "SELECT COUNT(*) FROM " . $ecs->table('cart') .
        " WHERE session_id = '" . SESS_ID . "'";
    if ($db->getOne($sql) == 0)
    {
        show_message("没有选择任何服务", '', '', 'warning');
    }

    /*
     * 检查用户是否已经登录
     * 如果用户已经登录了则检查是否有默认的收货地址
     * 如果没有登录则跳转到登录和注册页面
     */
    if ($_SESSION['user_id'] == 0)
    {
        /* 用户没有登录且没有选定匿名购物，转向到登录页面 */
        ecs_header("Location: flow.php?step=login&back_act=flow.php\n");
        exit;
    }

    $order = array(
        'pay_id'          => intval($_POST['payment']),
        'user_id'         => $_SESSION['user_id'],
        'add_time'        => gmtime(),
        'order_status'    => OS_UNCONFIRMED,
        'shipping_status' => SS_UNSHIPPED,
        'pay_status'      => PS_UNPAYED,
        'postscript'      => trim(htmlspecialchars($_POST['postscript'])),
        );

    /* 订单中的商品 */
    $cart_service = cart_service();

    if (empty($cart_service))
    {
        show_message("没有选择任何服务", $_LANG['back_home'], './', 'warning');
    }

    /* 订单中的总额 */
    $total = order_fee($cart_service);
    $order['order_amount']  = $order["goods_amount"] = number_format($total['amount'], 2, '.', '');
	
    /* 插入订单表 */
    $error_no = 0;
    do
    {
        $order['order_sn'] = get_order_sn(); //获取新订单号
        $GLOBALS['db']->autoExecute($GLOBALS['ecs']->table('order_info'), $order, 'INSERT');

        $error_no = $GLOBALS['db']->errno();

        if ($error_no > 0 && $error_no != 1062)
        {
            die($GLOBALS['db']->errorMsg());
        }
    }
    while ($error_no == 1062); //如果是订单号重复则重新提交数据

    $new_order_id = $db->insert_id();
    $order['order_id'] = $new_order_id;

    /* 插入订单商品 */
    $sql = "INSERT INTO " . $ecs->table('order_goods') . "( " .
                "order_id, service_name, price, title, content, retime, essay_file, resume_file, ".
                "source, type, reprice, wordnumber, email, remark, mobile, contact_type, contact_time, contact, school, major) ".
            " SELECT '$new_order_id', service_name, price, title, content, retime, essay_file, resume_file, ".
                "source, type, reprice, wordnumber, email, remark, mobile, contact_type, contact_time, contact, school, major".
            " FROM " .$ecs->table('cart') .
            " WHERE session_id = '".SESS_ID."'";
    $db->query($sql);
    
    /* 增加是否给客服发送邮件选项 */
    if (1)
    {
        $tpl = get_mail_template('order_confirm');
        $smarty->assign('order', $order);
        $smarty->assign('shop_name', $_CFG['shop_name']);
        $smarty->assign('send_date', date($_CFG['time_format']));
        $content = $smarty->fetch('str:' . $tpl['template_content']);
        send_mail($_CFG['shop_name'], $_SESSION["user_name"], $tpl['template_subject'], $content, $tpl['is_html']);
    }

    /* 清空购物车 */
    clear_cart();
    /* 清除缓存，否则买了商品，但是前台页面读取缓存，商品数量不减少 */
    clear_all_files();

    /* 插入支付日志 */
    $order['log_id'] = insert_pay_log($new_order_id, $order['order_amount'], PAY_ORDER);

    /* 取得支付信息，生成支付代码 */
    if ($order['order_amount'] > 0)
    {
        $payment = payment_info($order['pay_id']);

        include_once('includes/modules/payment/' . $payment['pay_code'] . '.php');

        $pay_obj    = new $payment['pay_code'];

        $pay_online = $pay_obj->get_code($order, unserialize_config($payment['pay_config']));

        $order['pay_desc'] = $payment['pay_desc'];

        $smarty->assign('pay_online', $pay_online);
    }
    if(!empty($order['shipping_name']))
    {
        $order['shipping_name']=trim(stripcslashes($order['shipping_name']));
    }

    /* 订单信息 */
    $smarty->assign('order',      $order);
    $smarty->assign('total',      $total);
    $smarty->assign('order_submit_back', sprintf($_LANG['order_submit_back'], $_LANG['back_home'], $_LANG['goto_user_center'])); // 返回提示

    unset($_SESSION['flow_order']);
    unset($_SESSION['direct_shopping']);
}

/*------------------------------------------------------ */
//-- 删除购物车中的商品
/*------------------------------------------------------ */

elseif ($_REQUEST['step'] == 'deleteItem')
{
    $rec_id = intval($_GET['id']);
   // 删除cart中无效的数据
    $sql = "SELECT resume_file, essay_file FROM " . $GLOBALS["ecs"]->table("cart") . " WHERE session_id = '" . SESS_ID . "' AND rec_id='{$rec_id}'";
    $res = $GLOBALS['db']->query($sql);
    while ($row = $GLOBALS['db']->fetch_Array($res))
    {
    	if(@is_file($row["resume_file"]))
    	{
    		@unlink($row["resume_file"]);
    	}
    	if(@is_file($row["essay_file"]))
    	{
    		@unlink($row["essay_file"]);
    	}
    }
    
    $sql = "DELETE FROM " . $GLOBALS['ecs']->table('cart') .
            "  WHERE session_id = '" . SESS_ID . "' AND rec_id='{$rec_id}'";
    $GLOBALS['db']->query($sql);
    ecs_header("Location: flow.php\n");
    exit;
}
$smarty->assign('currency_format', $_CFG['currency_format']);
$smarty->assign('integral_scale',  $_CFG['integral_scale']);
$smarty->assign('step',            $_REQUEST['step']);
assign_dynamic('shopping_flow');

$smarty->display('flow.dwt');


function final_price($num, $type)
{
		$sql = "SELECT * FROM " . $GLOBALS["ecs"]->table("service") . " WHERE type = '$type'";
		$info = $GLOBALS["db"]->getRow($sql);
		$reprice = $info["urgency"];
		$base_price = 0;
		if($type == "essay" || $type == "chenshu")
		{
			$price = @unserialize($info["price"]);
			$minletter = @unserialize($info["minletter"]);
			$maxletter = @unserialize($info["maxletter"]);
			
			foreach($minletter as $key =>$val)
			{
				if($num>=$val && $num<=$maxletter[$key])
				{
					$base_price = $price[$key];
				}
			}
		}
		else
		{
			$base_price = $info["price"];
		}
		return array("base_price" => $base_price, "reprice" => $reprice);
}

function modify_cart($id, $field)
{
	if(!$id) return;
	$sql = "SELECT * FROM " . $GLOBALS['ecs']->table("cart") . " WHERE rec_id = '{$id}'";
	$info = $GLOBALS['db']->getRow($sql);
	if(is_file($info[$field]))
	{
		@unlink($info[$field]);
	}
}

function get_temp_file($type)
{
	$sql = "SELECT * FROM " . $GLOBALS['ecs']->table("temp_file") . " WHERE type = '{$type}' AND session_id = '".SESS_ID."'";
	return $GLOBALS['db']->getRow($sql);
}

function clear_temp_file($type)
{
	$GLOBALS['db']->query("DELETE FROM " . $GLOBALS["ecs"]->table("temp_file") . " WHERE type = '{$type}' AND session_id = '".SESS_ID."'");
}
?>